package com.example.fragment;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.RoomSQLiteQuery;
import androidx.room.util.DBUtil;

import com.example.activity.MainActivity;
import com.example.adapter.CourseListAdapter;
import com.example.room.ZRoomDB;
import com.example.room.course.Course;
import com.example.xc.R;
import com.example.room.DbManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程列表适配器
 */
public class CourseListFragment extends Fragment {

    CourseListAdapter adapter;

    View rootView;

    /**
     * 本方法用于创建Fragment布局
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.course_list_fragment, container, false);            //关联布局文件
        return rootView;
    }

    /**
     * 本方法用于显示页面时刷新数据
     */
    @Override
    public void onResume() {
        super.onResume();
        initRecyclerView(rootView);
        MainActivity.initHomeBtnColor();
    }

    /**
     * 本方法用于实现RecyclerView+StaggeredGridLayout流式列表效果
     *
     * @param view 传入新品布局对象
     */
    @SuppressLint({"Range","RestrictedApi"})
    private void initRecyclerView(View view) {
        List<Course> desks = new ArrayList<>();
        RecyclerView rv = view.findViewById(R.id.newRecycler);                                        //获得RecyclerView控件对象
        //设置为StaggeredGridLayout流式布
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        Cursor cursor = null;
        try {
            ZRoomDB zRoomDB = DbManager.INSTANCE.getDB(requireContext());
            final String _sql = "SELECT * FROM COURSE";
            final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
            zRoomDB.assertNotSuspendingTransaction();
            cursor = DBUtil.query(zRoomDB, _statement, false, null);
            if (cursor == null) {
                return;
            }
            while (cursor.moveToNext()){
                int id=cursor.getInt(cursor.getColumnIndex("id"));
                String name=cursor.getString(cursor.getColumnIndex("name"));
                String number=cursor.getString(cursor.getColumnIndex("number"));
                desks.add(new Course(id,number,name));
            }
        } catch (Exception e) {
            Log.e("NewFragment", "error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        adapter = new CourseListAdapter(getActivity(), desks);                                //调用适配器
        rv.setAdapter(adapter);                                                     //设置适配器到RecyclerView
    }
}
